(1)打開$person的'programs'(key) 抓 $language(vaule)
<?php
$person['name'] = 'Brad';
$person['age'] = 18;
$person['weight'] = 80;
$person['programs'] = ['PHP', 'Java', 'C', 'Swift'];
foreach ($person['programs'] as $language) {
echo "{$language}<br />";
}
(2)$p 抓 $key(key) 及 $value(vaule)
foreach ($p as $key => $value) {
echo "{$key}點出現{$value}次<br />";
}
想知道這是甚麼,可以跑這個迴圈慢慢拆(可以把Array都打開)
foreach ($_SERVER as $key => $value) {
//有array繼續打開
if (gettype($value) == 'array') {
echo "{$key} => ";
foreach ($value as $kk => $vv) {
echo "{$kk} : {$vv}<br />";
}
echo "===<br />";
} else {
echo "{$key} : {$value}<br />";
}
}
$ _ 還有很多變數可以用ex:$_GET、$_COOKIE...
(1)看全部有幾個? echo func_num_args()
sayHelloV4('Brad', 'Eric', 'Amy');
function sayHelloV4()
{
echo func_num_args() //3
}
(2)第幾個是誰? echo func_get_arg(0)
function sayHelloV4()
{
echo func_get_arg(0); //Brad
echo func_get_arg(1); //Eric
echo func_get_arg(2); //Amy
}
(3)看全部,使用foreach
$args = func_get_args();
function sayHelloV4()
{
foreach ($args as $name) {
echo "Hello, {$name}<br />";
}
}
(4)與var_dump 的差異
function sayHelloV4()
{
var_dump($args);
}
//array(3) { [0]=> string(4)
//"Brad" [1]=> string(4) "Eric" [2]=> string(3)
// "Amy" } Hello, Brad
https://www.php.net/manual/en/ref.strings.php
$mystr = 'abcdefg';
$findme = 'd';
if (strpos($mystr, $findme) !== false) { // !== false 全不等
//(TURE,非0、NULL...)
echo "find: " . strpos($mystr, $findme); //.字串相加
} else {
echo "not found";
}
!== false
全不等於,才不會進來判斷
if (strpos($mystr, $findme) !== false) {
echo "{$mystr}.{$findme}<br>";
echo "find: " . strpos($mystr, $findme); //.字串相加
} else {
echo "not found";
}
if (strlen($id) == 10) { //長度=10
if (strpos('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substr($id, 0, 1)) !== false)
// strpos $id第一碼(substr)的字在字串的第幾個位置(回傳int)
// substr 從0開始 , 第一碼
// 如果!== false(0)
// 輸出OK
{
echo 'ok';
} else {
echo 'X2';
}
} else {
echo 'X1';
}
https://www.php.net/manual/en/function.preg-match
https://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F
$id = 'A123456789';
$regex = '/^[A-Z][12][0-9]{8}$/'; //[12]1或2 {8}8碼
// $id 是否符合 $regex
if (preg_match($regex, $id)) {
echo 'OK';
} else {
echo 'XX';
}
include "bradapis.php";
include "myphp/bradapis.php"; //可以從不同資料夾
舉例:身分證號碼隨機產生
4.的限制最多,所以以他為依歸,其他1.2.3.例子引用4.
這樣就只維護4.就行了~
1.亂數
function createTWIdByRandom()
{
$gender = rand(0, 1) == 0;
return createTWIdByGender($gender);
}
2.指定性別
function createTWIdByGender($gender = false)
{
$letters = 'ABCDEFGHJKLMNPQRSTUVXYWZIO';
$area = substr($letters, rand(0, 25), 1);
return createTWIdByBoth($gender, $area);
}
3.指定地區
function createTWIdByArea($area = 'B')
{
$gender = rand(0, 1) == 0;
return createTWIdByBoth($gender, $area);
}
4.性別跟地區都指定
function createTWIdByBoth($gender, $area)
{
}
<input type="submit" value="Register" />
$_GET 只收 method="get" 密碼顯示瀏覽器,好處:開發看的到
$_POST 只收 method="post" 密碼不顯示
$_REQUEST 收 method="get" method="post"
https://www.php.net/manual/en/refs.fileprocess.file.php
用phpinfo();
檢查自己PHP的File System有沒有安裝或是目前版本
opendir - 開啟檔案
https://www.php.net/manual/zh/function.opendir.php
readdir - 讀取檔案(只讀同一層)
https://www.php.net/manual/zh/function.readdir.php
closedir - 關閉檔案
https://www.php.net/manual/zh/function.closedir
exit - 讀不到檔案會顯示
https://www.php.net/manual/zh/function.exit
while ($row = readdir($fp)) {
}